<?php
    require_once("SmartyReady.php");
    require_once("usuario.php");
    require_once("mp3.php");
    require_once("playlists.php");
    // preparem l'smarty
    $sm = new SmartyReady();
    $sm->muntarTemplates();
    // preparem la sessio
    session_start();
    if (!empty($_SESSION['username'])) {
        $sm->assign('banner','bannerUser.tpl');
        $sm->assign('usuari',$_SESSION['username']);
        $sm->assign('pathAvatar',$_SESSION['avatar']);
        $sm->assign('cuerpo','editarplaylist.tpl');
        $sm->assign('playlist',false);
        $sm->assign('songs',false);
        // busquem les playlists de l'usuari i les parsejem
        $user = Usuario::findByUsername($_SESSION['username']);
        $iduser = $user->get_id();
        $array_playlists = Playlists::findbyidusuari($iduser);

        $noms_playlists = Array();
        $idsplaylists = Array();
        $sm->assign('nomplay',"");
        if(count($array_playlists) > 0)
        {
            foreach($array_playlists as $pl)
            {
                $noms_playlists[] = $pl->get_nom();
                $idsplaylists[$pl->get_nom()] = $pl->get_id();
            }
        }
        $sm->assign('playlist',$noms_playlists);
        $sm->assign('nplaylist','');
        if (empty($_GET['npl']) && empty($_GET['ns'])) {
             $sm->assign('get',false);
             if (!empty($_POST['priv']))  {
                $mp3 = $_SESSION['mp3'];
                $user = Usuario::findByUsername($_SESSION['username']);
                $iduser = $user->get_id();
                if (!$mp3->get_publicusuari($iduser)) {
                    $mp3->set_public($iduser);
                }
                else {
                     $mp3->set_private($iduser);
                }
             }
             else if (!empty($_POST['esb'])) {
                 $mp3 = $_SESSION['mp3'];
                 $idpl = $_SESSION['idplaylist'];
                 $playlist = new Playlists($idpl);
                 $playlist->delete_song($mp3->get_id());
                 
            }
            else if (!empty($_POST['delplay'])) {
                
                $playlist = new Playlists($_SESSION['idplaylist']);
                $playlist->delete();
                ?><meta http-equiv="Refresh" content="0;url=./editplaylist.php"><?php
            }
            else if (!empty($_POST['web'])) {
                $mp3 = $_SESSION['mp3'];
                $id_mp3 = $mp3->get_id();
                $artista = $mp3->get_artista();
                $album2 = $mp3->get_album();
                echo "<meta http-equiv='Refresh' content='0;url=./webservice.php?art=".$artista."&alb=".$album2."&id=".$id_mp3."'>";



            }
        }
        else if (!empty($_GET['npl'])) {
            $ide = $idsplaylists[$_GET['npl']];
            $_SESSION['idplaylist'] = $ide;
            $pleilist = new Playlists($ide);
            // Busquem les cançons de la playlist i les parsejem
            $array_songs = $pleilist->songs();
            $array_song_names = Array();
            $array_song_id = Array();
            $sm->assign('nplaylist',$_GET['npl']);
            if (count($array_songs) > 0) {
                foreach($array_songs as $s) {
                    $array_song_names[] = $s->get_titol();
                    $array_song_id[$s->get_titol()] = $s->get_id();
                }
                $sm->assign('songs',$array_song_names);
            }
            else $sm->assign('songs',false);
            $sm->assign('playlist',false);
            $sm->assign('get',true);
            $_SESSION['songs'] = $array_song_id;
        }
        else if ($_GET['ns']) {
            $sm->assign('playlist',false);
            $sm->assign('get',false);
            $mp3 = new Mp3($_SESSION['songs'][$_GET['ns']]);
            $sm->assign('titolsong',$mp3->get_titol());
            $sm->assign('artista',$mp3->get_artista());
            $sm->assign('any',$mp3->get_year());
            $sm->assign('track',$mp3->get_track());
            $sm->assign('nalbum',$mp3->get_album());
            $sm->assign('path',$mp3->get_caratula());
            $user = Usuario::findByUsername($_SESSION['username']);
            $iduser = $user->get_id();
            $priv = $mp3->get_publicusuari($iduser);
            if ($priv) $sm->assign('priv','Publica');
            else $sm->assign('priv','Privada');
            $sm->assign('cuerpo','dadesong.tpl');
            $_SESSION['mp3'] = $mp3;
        }
    }
    else {
        // T'has colat a la pagina XXX sense estar logejat
        $sm->assign("banner","login.tpl");
        $sm->assign('resultatLogin','');
        $sm->assign('cuerpo','buit.tpl');
        ?><meta http-equiv="Refresh" content="0;url=./index.php"><?php
    }
    $sm->display("E:/wamp/www/JediMusic/Smarty/templates/base.tpl");
?>

